package com.kuang.pool;


import java.util.concurrent.*;

/**
 * **
 * * new ThreadPoolExecutor.AbortPolicy() // 银行满了，还有人进来，不处理这个人的，抛出异
 * 常
 * * new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里！
 * * new ThreadPoolExecutor.DiscardPolicy() //队列满了，丢掉任务，不会抛出异常！
 * * new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了，尝试去和最早的竞争，也不会
 * 抛出异常
 */
public class demo2 {
    public static void main(String[] args) {
        ExecutorService pool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        try {
// 最大承载：Deque + max
// 超过 RejectedExecutionException
            for (int i = 1; i <= 9; i++) {
// 使用了线程池之后，使用线程池来创建线程
                pool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
// 线程池用完，程序结束，关闭线程池
            pool.shutdown();
        }
    }


}
